﻿#include "jzcommand.h"
#include <QStringList>

void Jzcommand::docommand(QString data)
{
    //    下位机回应运行状态
    //    com4 => com3
    //    MD-11客户端（上位机）=> 检针机（下位机）
    //    AA 19 0A 12 34 80 01 02 00 AB 3E
    //    80 -- 停止
    //    81 -- 运行
    //    00 -- 正常通过
    //    02 -- 误报
    //    03 -- 有针
    //    合格数=0102H，检出数=00ABH
    QStringList cmdlist = data.split("AA ");
    for (int i = 0; i < cmdlist.count(); i++) {
        //QString strB = "AA 19 0A 12 34 80 01 02 00 AB 3E";
        QString strB = cmdlist[i];
        if (strB.length() < 20) {
            continue;
        }
        QStringList list = strB.split(" ");
        if (list.length() < 10) {
            continue;
        }

        if (list[0] == "01" || list[0] == "06" || list[0] == "10") {
            continue;
        }

        if (list[4] == "80") {

        } else if (list[4] == "81") {

        } else if (list[4] == "01") {
            //正常才解析合格数，检出数（金属数）
            //zongshu = hege + jinshu;
            int hege = 0;
            int jcs = 0;
            QString strHege = list[5] + list[6];
            QString strJcs = list[7] + list[8];
            bool ok;
            hege = strHege.toInt(&ok, 16);
            jcs = strJcs.toInt(&ok, 16);

        } else if (list[4] == "02") {

        } else if (list[4] == "03") {

        }

//        int a = 0;
//        for (int x = 0; x < list.count(); x++) {
//            bool ok;
//            a +=list[x].toInt(&ok, 16);
//        }
    }
}

void Jzcommand::sendcmd(QString data)
{

}

//呼叫下位机
//com3 => com4
//MD-11客户端（上位机）=> 检针机（下位机）
//AA 19 06 12 34 82 6E

QString Jzcommand::RCR(QString strB)
{
   //QString strB = "AA 19 06 12 34 82";
    QStringList list = strB.split(" ");
    int a = 0;
    for (int x = 0; x < list.count(); x++) {
        bool ok;
        a +=list[x].toInt(&ok, 16);
    }

    a = ~a;//401   -402
    QString s = QString::number(a, 16);
    s = s.mid(s.length() - 2, 2);
    s = s.toUpper();
    return s;
}

//获取重置cmd
QString Jzcommand::getczcmd(int hege, int jinshu)
{
    QString hegeX = QString::number(hege, 16).asprintf("%04X", hege).toUpper();
    QString jinshuX = QString::number(jinshu, 16).asprintf("%04X", jinshu).toUpper();

    QString gw = hegeX.mid(0, 2);
    QString dw = hegeX.mid(2, 2);
    QString gw1 = jinshuX.mid(0, 2);
    QString dw1 = jinshuX.mid(2, 2);

    QString ss = QString("AA 06 09 12 34 %1 %2 %3 %4").arg(gw, dw, gw1, dw1);
    ss = QString("%1 %2").arg(ss, Jzcommand::RCR(ss));

    return ss;
}

//返回主界面后发给检针机的未知信息 -- 启动检针机
QString Jzcommand::writexulie()
{
    return QString("AA 10 05 12 34 FA");
}

//呼叫检针机
QString Jzcommand::calljzj()
{
    return QString("AA 19 06 12 34 82 6E");
}



